/**@author: Administrator
 * @date: 2013-2-25
 * @Description:
 */
package org.benxiong.iplay;

import org.benxiong.iplay.model.Message;
import org.benxiong.iplay.util.Constants;
import org.benxiong.iplay.util.GlobalVar;
import org.benxiong.iplay.util.LogX;
import org.benxiong.iplay.util.ObjectConverter;
import org.benxiong.iplay.util.PreferencesUtil;
import org.benxiong.iplay.util.ServerUtilities;

import android.content.Context;
import android.content.Intent;

import com.google.android.gcm.GCMBaseIntentService;

/**
 * @author Administrator
 *
 */
public class GCMIntentService extends GCMBaseIntentService {
	
	public GCMIntentService() {
		super(Constants.GCM_SENDER_ID);
	}
	/* (non-Javadoc)
	 * @see com.google.android.gcm.GCMBaseIntentService#onError(android.content.Context, java.lang.String)
	 */
	@Override
	protected void onError(Context arg0, String arg1) {
		// TODO Auto-generated method stub
		LogX.print("error occurs=========");
	}

	/* (non-Javadoc)
	 * @see com.google.android.gcm.GCMBaseIntentService#onMessage(android.content.Context, android.content.Intent)
	 */
	@Override
	protected void onMessage(Context context, Intent intent) {
		Message message = ObjectConverter.intentToMessage(intent);
		LogX.print("recived push message: " + message);
		// TODO: update current dialog (if exist) and conversation
	}

	/* (non-Javadoc)
	 * @see com.google.android.gcm.GCMBaseIntentService#onRegistered(android.content.Context, java.lang.String)
	 */
	@Override
	protected void onRegistered(Context context, String regId) {
		// TODO Auto-generated method stub
		LogX.print("registered=========id:"+regId);
		GlobalVar.tokenId = regId;
		boolean isOk = ServerUtilities.register(context, regId);
		if(isOk) {
			PreferencesUtil.saveGCMRegInfo();
		}
	}

	/* (non-Javadoc)
	 * @see com.google.android.gcm.GCMBaseIntentService#onUnregistered(android.content.Context, java.lang.String)
	 */
	@Override
	protected void onUnregistered(Context arg0, String arg1) {
		// TODO send request to pull registerId from user
		LogX.print("unregistered=========");
	}

}
